import { plan_view_list, view_info } from '@/api/plan_view'

const getDefaultState = () => {
  return {
    viewTitle: '请选择视图',
    showViewDropdown: false,
    viewList: [],
    currentViewId: 0,
    currentViewPath: ''
  }
}

const state = getDefaultState()

const mutations = {
  SET_VIEW_LIST: (state, list) => {
    state.viewList = list
  },
  SET_VIEW_TITLE: (state, title) => {
    state.viewTitle = title
  },
  SET_VIEW_ID: (state, view_id) => {
    state.currentViewId = view_id
  },
  SET_VIEW_PATH: (state, path) => {
    state.currentViewPath = path
  }
}

const actions = {
  getViewList({ commit }, project_id) {
    commit('SET_VIEW_LIST', [])
    commit('SET_VIEW_ID', 0)
    commit('SET_VIEW_PATH', '')
    commit('SET_VIEW_TITLE', '请选择视图')
    return new Promise((resolve, reject) => {
      plan_view_list(project_id).then(response => {
        const { data } = response
        commit('SET_VIEW_LIST', data.list)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  getViewInfo({ commit }, view_id) {
    return new Promise((resolve, reject) => {
      view_info(view_id).then(response => {
        const { data } = response
        commit('SET_VIEW_TITLE', data.name)
        commit('SET_VIEW_ID', data.id)
        commit('SET_VIEW_PATH', data.path)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default { 
  namespaced: true,
  state,
  mutations,
  actions
}

